package com.sky.controller.admin;

import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @PostMapping
    public Result<String>save(@RequestBody CategoryDTO categoryDTO){
        log.info("save category: {}", categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }

    @GetMapping("/page")
    public Result<PageResult>pageQuery(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("page query: {}", categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping
    public Result deleteById(Long id){
        log.info("delete category: {}", id);
        categoryService.deleteById(id);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("start or stop category: id --> {}, status --> {}", id,status);
        categoryService.startOrStop(status,id);
        return Result.success();
    }


    @PutMapping
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("update category: {}", categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @GetMapping("/list")
    public Result<List<Category>> queryByType(Integer type){
        log.info("query by type --> {}", type);
        List<Category> categoryList = categoryService.queryByType(type);
        return Result.success(categoryList);
    }




}
